package com.bytz.modules.cms.reseller.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bytz.modules.cms.reseller.entity.EndUser;
import com.bytz.modules.cms.reseller.model.EndUserModel;
import com.bytz.modules.cms.reseller.model.SearchEndUserModel;
import com.bytz.modules.cms.reseller.service.IEndUserService;
import com.bytz.modules.cms.reseller.service.IMallEndUserService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallEndUserServiceImpl implements IMallEndUserService {

    @Autowired
    private IEndUserService endUserService;

    @Override
    public List<EndUserModel> getEndUserList() {

        String resellerId = ResellerSecurityUtils.getUserId();

        return endUserService.getByResellerId(resellerId, null);
    }

    @Override
    public SearchEndUserModel searchEndUser(String condition) {

        List<EndUser> list = endUserService.getByCustomerName(condition);

        if (CollectionUtil.isEmpty(list)) {

            return new SearchEndUserModel(null, "notFound");
        }

        if (list.size() == 1) {

            return new SearchEndUserModel(list.get(0), "success");
        }

        for (EndUser endUser : list) {

            if (condition.equals(endUser.getCustomerName()) ||
                    condition.equals(endUser.getCustomerNumber()) ||
                    condition.equals(endUser.getAddress())) {

                return new SearchEndUserModel(endUser, "success");
            }
        }

        return new SearchEndUserModel(null, "needImprovedCondition");
    }
}
